package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.pojo.Goods;
import com.qf.pojo.GoodsType;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    @Override
    public List<GoodsType> findTypeListByLevel(int level) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select id,name,level,parent from tb_goods_type where level = ?";
        try {
            List<GoodsType> goodsTypes = qf.query(sql, new BeanListHandler<>(GoodsType.class), level);
            return goodsTypes;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public GoodsType findById(int id) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select id,name,level,Parent  from tb_goods_type where id=? ";
        try {
            GoodsType goodsType= qf.query(sql,new BeanHandler<>(GoodsType.class),id);
            return goodsType;
        } catch (SQLException e) {

            e.printStackTrace();
        }
        return null;
    }
}
